package com.jiuyin.dianjing.util;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import static com.jiuyin.dianjing.BuildConfig.SHOW_LOG;
import static com.jiuyin.dianjing.constant.HelperConstant.TAG;

/**
 * @author Administrator
 */
public final class LogUtil {
    /**
     * add common dir  save some common log without pay type
     * eg: ui show error...
     *
     * @param content log content
     */
    public static void logCommon(final String content) {
        if (SHOW_LOG) {
            d(TAG, content);
        }
        PayFileUtil.saveLogFile(content);
    }

    public static void log(final String content) {
        if (SHOW_LOG) {
            d(TAG, content);
        }
        PayFileUtil.saveLogFile(content);
    }

    public static void logOnly(final String content) {
        if (SHOW_LOG) {
            d(TAG, content);
        }
    }


    public static void d(String tag, String msg) {  //信息太长,分段打印
        //因为String的length是字符数量不是字节数量所以为了防止中文字符过多，
        //  把4*1024的MAX字节打印长度改为2001字符数
        int max_str_length = 2001 - tag.length();
        //大于4000时
        while (msg.length() > max_str_length) {
            Log.d(tag, msg.substring(0, max_str_length));
            msg = msg.substring(max_str_length);
        }
        //剩余部分
        Log.d(tag, msg);
    }

    /**
     * 打印Intent
     *
     * @param intent
     */
    public static void printIntent(Intent intent) {
        if (null != intent) {
            Bundle bundle = intent.getExtras();
            if (null != bundle) {
                for (String key : bundle.keySet()) {
                    LogUtil.log("Key=" + key + ", content=" + bundle.getString(key));
                }
            }
        }
    }
}
